﻿namespace com.stifel.ServerBuildMgmt
{
    using System.Linq;
    using System.Reflection;
    using System.Xml;

    using com.stifel.Extensions.AssemblyExtensions;
    using com.stifel.Extensions.SharePointExtensions;
    using com.stifel.Extensions.SymbolExtensions;
    using com.stifel.ServerBuildMgmtDao;
    using com.stifel.SharePoint;

    using Microsoft.SharePoint;

    public class StifelVirtualServerRequest_Definition : BaseDefinition
    {
        #region Fields

        const string SITECOLUMNS = "Stifel-VirtualServerRequest.xml";

        #endregion Fields

        #region Constructors

        public StifelVirtualServerRequest_Definition( SPFeatureReceiverProperties properties )
            : base(properties)
        {
        }

        ~StifelVirtualServerRequest_Definition( )
        {
            Dispose( false );
        }

        #endregion Constructors

        #region Methods

        public override void Create( )
        {
            base.Create( );
            CreateSiteColumns( getFieldDefinitionsAsXml( SITECOLUMNS ) );
            CreateContentType( getContentTypeDefn( ) );
            CreateList( getListDefn( ) );
            loadList( );
            addEventReceiver( );
        }

        public override void Delete( )
        {
            base.Delete( );
            deleteEventReciever( );
            DeleteList( getListDefn( ), false );
            DeleteContentType( getContentTypeDefn( ) );
            DeleteSiteColumns( getFieldDefinitionsAsXml( SITECOLUMNS ) );
        }

        void addEventReceiver( )
        {
            using ( SPWeb spWeb = Properties.GetWeb( ) )
            {
                SPList spList = spWeb.Lists.GetListByRootFolder( StifelServerBuildDao.ListName );
                if ( spList != null )
                {
                    string assemblyFQN = Assembly.GetExecutingAssembly( ).FullName;
                    string classFQN = typeof( VirtualServerBuildRequestEventReceiver ).FullName;
                    spList.EventReceivers.Add( SPEventReceiverType.ItemAdded, assemblyFQN, classFQN );
                    spList.EventReceivers.Add( SPEventReceiverType.ItemUpdated, assemblyFQN, classFQN );
                }
            }
        }

        void deleteEventReciever( )
        {
            using ( SPWeb spWeb = Properties.GetWeb( ) )
            {
                SPList spList = spWeb.Lists.GetListByRootFolder( StifelServerBuildDao.ListName );
                if ( spList != null )
                {
                    for ( int i = spList.EventReceivers.Count - 1; i >= 0; i-- )
                    {
                        if ( spList.EventReceivers[ i ].Type.Equals( SPEventReceiverType.ItemUpdated ) )
                            spList.EventReceivers[ i ].Delete( );
                        else if ( spList.EventReceivers[ i ].Type.Equals( SPEventReceiverType.ItemAdded ) )
                            spList.EventReceivers[ i ].Delete( );
                    }
                }
            }
        }

        ContentTypeDefn getContentTypeDefn( )
        {
            StifelVirtualServerRequest c = null;
            return new ContentTypeDefn
            {
                Name = Item.GetContentTypeName<StifelVirtualServerRequest>( ),
                ContentTypeId = Item.GetContentTypeId<StifelVirtualServerRequest>( ),
                WebLevel = true,
                Group = "Stifel",
                Description = "Describes a virtual server request.",
                Fields = new FieldDefn[ ] {
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelVirtualServerRequest>(c.GetPropertySymbol(o=>o.ActivityAuditLog))
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelVirtualServerRequest>(c.GetPropertySymbol(o=>o.ADConfigNotificationDate))
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelVirtualServerRequest>(c.GetPropertySymbol(o=>o.ADConfigStatus))
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelVirtualServerRequest>(c.GetPropertySymbol(o=>o.ApprovalNotificationDate))
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelVirtualServerRequest>(c.GetPropertySymbol(o=>o.ApprovalStatus))
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelVirtualServerRequest>(c.GetPropertySymbol(o=>o.AVConfigNotificationDate))
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelVirtualServerRequest>(c.GetPropertySymbol(o=>o.AVConfigStatus))
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelVirtualServerRequest>(c.GetPropertySymbol(o=>o.BackupConfigNotificationDate))
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelVirtualServerRequest>(c.GetPropertySymbol(o=>o.BackupConfigStatus))
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelVirtualServerRequest>(c.GetPropertySymbol(o=>o.BuildNotificationDate))
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelVirtualServerRequest>(c.GetPropertySymbol(o=>o.BuildStatus))
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelVirtualServerRequest>(c.GetPropertySymbol(o=>o.Control))
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelVirtualServerRequest>(c.GetPropertySymbol(o=>o.MonitorConfigNotificationDate))
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelVirtualServerRequest>(c.GetPropertySymbol(o=>o.MonitorConfigStatus))
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelVirtualServerRequest>(c.GetPropertySymbol(o=>o.OverallStatus))
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelVirtualServerRequest>(c.GetPropertySymbol(o=>o.PatchConfigNotificationDate))
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelVirtualServerRequest>(c.GetPropertySymbol(o=>o.PatchConfigStatus))
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelVirtualServerRequest>(c.GetPropertySymbol(o=>o.RequestApproved))
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelVirtualServerRequest>(c.GetPropertySymbol(o=>o.RequestCanceled))
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelVirtualServerRequest>(c.GetPropertySymbol(o=>o.RequestCompleted))
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelVirtualServerRequest>(c.GetPropertySymbol(o=>o.RequestorNotificationDate))
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelVirtualServerRequest>(c.GetPropertySymbol(o=>o.SerializedVirtualServerRequest))
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelVirtualServerRequest>(c.GetPropertySymbol(o=>o.RequestRejected))
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelVirtualServerRequest>(c.GetPropertySymbol(o=>o.RequestorStatus))
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelVirtualServerRequest>(c.GetPropertySymbol(o=>o.NotificationId))
                    }
                }.Distinct( ).ToList( )
            };
        }

        XmlNodeList getFieldDefinitionsAsXml( string resource )
        {
            XmlNodeList fields = null;
            string s = Assembly.GetExecutingAssembly( ).GetFullyQualifiedManifestResourceName( resource );
            string xml = Assembly.GetExecutingAssembly( ).GetManifestResourceAsString( s );
            if ( !string.IsNullOrEmpty( xml ) )
            {
                XmlDocument d = new XmlDocument( );
                d.LoadXml( xml );
                XmlNode n = d.SelectSingleNode( "Fields" );
                fields = n.SelectNodes( "Field" );
            }
            return fields;
        }

        ListDefn getListDefn( )
        {
            return new ListDefn
            {
                RootFolderName = StifelServerBuildDao.ListName,
                Title = "Stifel Virtual Server Request",
                Description = "Contains the virtual server build requests.",
                ContentTypes = new string[ ] {
                    Item.GetContentTypeName<StifelVirtualServerRequest>()
                }.ToList( )
            };
        }

        void loadList( )
        {
        }

        #endregion Methods
    }
}